/* GUI CONVERTED from hkbmpkcca.p (converter v1.75) Sun Aug 13 21:22:52 2000 */
/* hkbmpkcca.p - QUERY SIMULATED PICKLIST Consider Onhand for Phantoms    */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Report                                        */
/*  REVISION: 5.0       LAST EDIT: 08/01/89      CREATED BY: BJJ *B215**/
/*  REVISION: 6.0       LAST EDIT: 05/18/90     MODIFIED BY: WUG *D002**/
/*  REVISION: 7.0       LAST EDIT: 10/26/92     MODIFIED BY: emb *G234**/
/*  REVISION: 7.4       LAST EDIT: 09/01/93     MODIFIED BY: dzs *H100**/
/*  REVISION: 7.2       LAST EDIT: 02/21/94     MODIFIED BY: ais *FM19**/
/*  REVISION: 7.2       LAST EDIT: 07/27/94     MODIFIED BY: qzl *H461**/
/*  REVISION: 7.2       LAST EDIT: 02/09/95     MODIFIED BY: qzl *F0HQ**/
/*  REVISION: 7.2       LAST EDIT: 03/17/95     MODIFIED BY: qzl *F0N3**/
/*  REVISION: 7.4       LAST EDIT: 12/19/95     MODIFIED BY: bcm *G1H5**/
/*  REVISION: 7.4       LAST EDIT: 01/23/96     MODIFIED BY: jym *G1JF**/
/*  REVISION: 7.4       LAST EDIT: 02/02/96     MODIFIED BY: jym *G1LP**/
/*  REVISION: 7.4   LAST EDIT: 02/04/97     MODIFIED BY: *G2J7* Murli Shastri */

/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* jyn                */

/* ********** Begin Translatable Strings Definitions ********* */


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

&SCOPED-DEFINE hkbmpkcca_p_1 "Use up Ph"
/* MaxLen: Comment: */

&SCOPED-DEFINE hkbmpkcca_p_2 "Op"
/* MaxLen: Comment: */

&SCOPED-DEFINE hkbmpkcca_p_3 "Net OH"
/* MaxLen: Comment: */

&SCOPED-DEFINE hkbmpkcca_p_4 "As of"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

define shared variable mfguser as character.
define shared variable site like in_site no-undo.
define variable phantom like pt_phantom.

define shared workfile pkdet no-undo
    /*fdn*/ field pkpar like ps_par
    field pkpart like ps_comp
    field pkop as integer format ">>>>>9"
    field pkstart like pk_start
    field pkend like pk_end
    field pkqty like pk_qty
    field pkbombatch like bom_batch initial 1
    field pkltoff like ps_lt_off.

define shared variable comp like ps_comp.
define shared variable net_oh like mfc_logical initial no label {&hkbmpkcca_p_3}.
define shared variable qty as decimal.
define shared variable use_up like mfc_logical initial yes label {&hkbmpkcca_p_1}.
define variable tmp_qty as decimal initial 1 no-undo.
define variable level as integer initial 1 no-undo.

define variable maxlevel as integer initial 99 no-undo.
define variable record as integer extent 100 no-undo.
define variable save_qty as decimal extent 100 no-undo.
define variable i as integer no-undo.

define variable effstart as date no-undo.
define variable effend as date no-undo.
define variable eff_start as date extent 100 no-undo.
define variable eff_end as date extent 100 no-undo.
define variable qty_oh like in_qty_oh.
define variable qty_all like in_qty_all.

define shared variable op as integer label {&hkbmpkcca_p_2} format ">>>>>9".
define variable phop like op.
define variable ph like mfc_logical initial yes.
define variable par like ps_par.
define variable assy_op like ps_op no-undo.
define shared variable eff_date as date label {&hkbmpkcca_p_4}.

{mfdel.i pk_det "where pk_user = mfguser"}
{mfdel.i pkdet}

hide message no-pause.

find first ps_mstr use-index ps_parcomp where ps_par = comp and ((ps_op = op) or (op = 0)) no-lock no-error.

repeat:
    if not available ps_mstr then do:
        repeat:
            level = level - 1.
            if level < 1 then leave.
            find ps_mstr where recid(ps_mstr) = record[level] no-lock no-error.
            comp = ps_par.
            qty = save_qty[level].
            if level = 1 then effstart = ?.
            else effstart = eff_start[level - 1].
      
            if level = 1 then effend   = ?.
            else effend   = eff_end[level - 1].
      
            find next ps_mstr use-index ps_parcomp where ps_par = comp and (ps_start <= eff_date or ps_start = ?)
                and (ps_end >= eff_date or ps_end = ?) and (( if level = 1 then  ps_op = op else true) or (op = 0)) no-lock no-error.
            if available ps_mstr then leave.
        end.
    end.

    if level < 1 then leave.

    if level = 1 then assign assy_op = ps_op.

    find pt_mstr no-lock where pt_part = ps_comp no-error.

    qty_oh = 0.

    find in_mstr no-lock where in_part = ps_comp and in_site = site no-error.
    if available in_mstr then do:
       qty_oh = in_qty_oh.
       qty_all = in_qty_all.
    end.

    phantom = no.

    find ptp_det no-lock where ptp_part = ps_comp and ptp_site = site no-error.
    if available ptp_det then do:
       phantom = ptp_phantom.
    end.
    else if available pt_mstr then phantom = pt_phantom.

    if ps_ps_code = "X" or (not available pt_mstr and ps_ps_code = "") or (phantom = yes and ps_ps_code = "") then do:
        record[level] = recid(ps_mstr).
        save_qty[level] = qty.

        eff_start[level] = max(effstart,ps_start).
        if effstart = ? then eff_start[level] = ps_start.
        if ps_start = ? then eff_start[level] = effstart.
        eff_end[level] = min(effend,ps_end).
        if effend   = ? then eff_end[level] = ps_end.
        if ps_end   = ? then eff_end[level] = effend.

        if level < maxlevel or maxlevel = 0 then do:
            comp = ps_comp.

            if available ptp_det and ptp_bom_code <> "" then comp = ptp_bom_code.
            else if available ptp_det and ptp_bom_code = "" then comp = ptp_part.
            else if not available ptp_det and available pt_mstr and pt_bom_code <> "" then comp = pt_bom_code.
            else if not available ptp_det and available pt_mstr and pt_bom_code = "" then comp = pt_part.

            qty = qty * ps_qty_per * (100 / (100 - ps_scrp_pct)).
            tmp_qty = qty.

            if phantom = yes and qty_oh > 0 and use_up and ps_ps_code <> "X" then do:
                find first pk_det where pk_user = mfguser and pk_part = pt_part exclusive-lock no-error.
                if not available pk_det then do:
                    create pk_det.
                    pk_user = mfguser.
                    pk_part = pt_part.
                    if net_oh then pk_qty  = max(0,qty_oh - qty_all).
                    else pk_qty  = max(0,qty_oh).
                end.
                qty = max(0,qty - pk_qty).
                pk_qty = max(0,pk_qty - tmp_qty).
                phop = ps_op.
            end.

            effstart = max(eff_start[level],ps_start).
            if eff_start[level] = ? then effstart = ps_start.
            if ps_start = ?         then effstart = eff_start[level].
            effend = min(eff_end[level],ps_end).
            if eff_end[level] = ?   then effend = ps_end.
            if ps_end = ?           then effend = eff_end[level].

            if ps_ps_code <> "X" then do:
                create pkdet.
                assign 
                    /*fdn*/ pkpar = ps_par
                    pkpart = ps_comp
                    pkqty  = tmp_qty
                    pkstart = max(effstart,ps_start)
                    pkend   = min(effend,ps_end)
                    pkltoff = ps_lt_off
                    pkop = assy_op.

                if effstart = ? then pkstart = ps_start.
                if ps_start = ? then pkstart = effstart.
                if effend = ? then pkend = ps_end.
                if ps_end = ? then pkend = effend.
            end.
            phop = ps_op.
            par = ps_comp.

            level = level + 1.
            find first ps_mstr use-index ps_parcomp where ps_par = comp and (ps_start <= eff_date or ps_start = ?)
                and (ps_end >= eff_date or ps_end = ?) and (( if level = 1 then  ps_op = op else true) or (op = 0)) no-lock no-error.
        end.
        else do:
            find next ps_mstr use-index ps_parcomp where ps_par = comp and (ps_start <= eff_date or ps_start = ?)
                and (ps_end >= eff_date or ps_end = ?) and (( if level = 1 then  ps_op = op else true) or (op = 0)) no-lock no-error.
        end.
    end.
    else do:
        if ps_ps_code = "" and ps_qty_per <> 0 then do:
            create pkdet.
            assign 
                /*fdn*/ pkpar = ps_par
                pkpart = ps_comp
                pkqty  = ps_qty_per * qty * (100 / (100 - ps_scrp_pct))
                pkstart = max(effstart,ps_start)
                pkend   = min(effend,ps_end)
                pkltoff = ps_lt_off
                pkop = assy_op.

            if effstart = ? then pkstart = ps_start.
            if ps_start = ? then pkstart = effstart.
            if effend = ? then pkend = ps_end.
            if ps_end = ? then pkend = effend.
        end.
        find next ps_mstr use-index ps_parcomp where ps_par = comp and (ps_start <= eff_date or ps_start = ?)
            and (ps_end >= eff_date or ps_end = ?) and (( if level = 1 then  ps_op = op else true) or (op = 0)) no-lock no-error.
    end.
end.
